<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="30%"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="70px" :rules="rules">
      <el-form-item label="姓名" prop="dname">
        <el-input v-model="form.dname" />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="form.age" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input v-model="form.gender" />
      </el-form-item>
      <el-form-item label="介绍" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch } from 'vue'
import { editDoctorInfo } from '@/api/doctorInfo.js'
import i18n from '@/i18n'
import { ElMessage } from 'element-plus'
const props = defineProps({
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogTableValue: {
    type: Object,
    default: () => {}
  }
})

const formRef = ref(null)
const form = ref({
  dname: '',
  age: '',
  gender: '',
  description: '',
  account: ''
})
const emits = defineEmits(['update:modelValue', 'initUserList'])
const userData = JSON.parse(localStorage.getItem('Doctoraccount'))
const handleClose = () => {
  emits('update:modelValue', false)
}

const handleConfirm = async () => {
  await editDoctorInfo({ ...form.value, account: userData + '' })
  ElMessage({
    message: i18n.global.t('操作成功'),
    type: 'success'
  })
  emits('initUserList')
  handleClose()
}
const rules = ref({
  dname: [
    { required: true, message: 'Please input username', trigger: 'blur' }
  ],
  age: [{ required: true, message: 'Please input password', trigger: 'blur' }],
  gender: [
    { required: true, message: 'Please input password', trigger: 'blur' }
  ],
  description: [
    { required: true, message: 'Please input mobile', trigger: 'blur' }
  ]
})

watch(
  () => props.dialogTableValue,
  () => {
    form.value = props.dialogTableValue
  },
  { deep: true, immediate: true }
)
</script>

<style></style>